window.ims = window.ims || {};
/**
*作者 ：Fantasy
*Email: fantasycs@163.com
*QQ   : 8635335
*Blog : http://www.blogjava.net/fantasy
*版本 ：V1.0
*说明 ：异步分页条
*/
ims.PagerUtil ={
	init:function( option ){
		var pager = new ims.Pager();
		pager.start(option);
		return pager;
	}
};

// 定义分页条 看看最下面 jQuery.extend(ims.Pager.prototype,ims.AbstractPager);
ims.Pager = function(){};
// 参数配置
// 计算总数的Action 返回{count:100}这样的格式;
ims.Pager.countAction = ctx + "/query/count.do";
// 上一页图片
ims.Pager.imgPrev = ctx + "/js/pager/prev.gif";
// 下一页图片
ims.Pager.imgNext = ctx + "/js/pager/next.gif";
// 第一页图片
ims.Pager.imgFirst = ctx + "/js/pager/first.gif";
// 最后一页图片
ims.Pager.imgLast = ctx + "/js/pager/last.gif";
// 跳转图片
ims.Pager.imgGoto = ctx + "/js/pager/goto.gif";
// Excel图片
ims.Pager.imgExcel = ctx + "/js/pager/excel.gif'";
// 加载分页条的等待图片
ims.Pager.imgLoading = ctx + "/js/pager/loading.gif";
// 刷新按钮图片
ims.Pager.imgReload = ctx + "/js/pager/reload.gif";
// 共236条记录 共5页 当前第2页 236,5,2的样式
ims.Pager.cssNumber = "font-weight:bold;font-size:12px;color:red";
// 图片的样式
ims.Pager.imgCss = "border:0;margin-top:2px;margin-left:5px";
// 跳转输入框的样式
ims.Pager.cssInput = "height:15px;width:10px";
// 分页条的样式
ims.Pager.cssPager = "font-size:12px;background-color:#F4F8FB;color:#6E6E6E;text-align:right";
// 分页条状态
ims.Pager.statusComplete = "complete";

// 定义抽象分页条
ims.AbstractPager = {};
// 是否允许分页
ims.AbstractPager.isPage = true;
// 每页显示记录数
ims.AbstractPager.pageSize = 50;
// 开始记录数
ims.AbstractPager.pageStart = 1;
// 当前页
ims.AbstractPager.curPage = 1;
// 总记录数
ims.AbstractPager.pageCount = 0;
// 当页显示的记录数
ims.AbstractPager.dataSize = 0;
// 分页调用URL
ims.AbstractPager.action = "";
// 定义formID
ims.AbstractPager.formID = "FORM_" + new Date().getTime();
//计算总数SQL
ims.AbstractPager.sql = "select 0 from dual";
// 查询参数
ims.AbstractPager.parameter = "";
// 分页条ID
ims.AbstractPager.pagerID = "PAGER_" + new Date().getTime();
// 计算总数的Action 返回{count:100}这样的格式;
ims.AbstractPager.countAction = "";
// 分页条状态
ims.AbstractPager.status = "loading";// complete
// 开始
ims.AbstractPager.start = function( param ){
	this.init(param);
	this.prepare();
	
};
// 初始化
ims.AbstractPager.init = function( param ){
	this.isPage = param.isPage;
	this.pageSize = param.pageSize || this.pageSize;
	this.pageStart = param.pageStart || this.pageStart;
	this.pageCount = param.pageCount || 0;
	this.dataSize = param.dataSize || this.dataSize;
	this.pagerID = param.pagerID || this.pagerID;
	this.sql = param.sql || this.sql;
	this.parameter = param.parameter || this.parameter;
	this.action = param.action || "";
	this.countAction = param.countAction || ims.Pager.countAction;
	this.curPage = this.getCurPage();
	this.pages = [50,100,200,500,1000];
	// 如果不分页
	if( !this.isPage ){
		this.pageSize = this.dataSize;
		this.pageCount = this.dataSize;
		this.pages = [this.dataSize];
	}
};

//  准备工作
ims.AbstractPager.prepare = function(){
	var _this = this;
	var state = _this.getState();
	if( state.isQry && this.isPage){
		_this.printLoading();
		var param = {"sql":_this.sql,"pageId":jQuery("#pageId").val()};
		jQuery.post(_this.countAction, param , function( text ){
			var json = eval("(" + text + ")");
			_this.pageCount = json.count;
			_this.print();
		});
	}else{
		_this.pageCount = state.count;
		_this.print();
	}
};

// 取得当前页面状态
ims.AbstractPager.getState = function(){
	var t = this;
	if( t.pageCount == 0 ){
		if(t.curPage == 1 && t.dataSize < t.pageSize){
			return {count:t.dataSize,isQry:false};
		}else{
			return {count:0,isQry:true};
		}
	}else{
		return {count:t.pageCount,isQry:false};
	}
};

// 输出分页条
ims.AbstractPager.print = function(){
	this.buildSubmit();
	this.printPager();
	this.bindEvent();
	this.complete();
};

// 加载完成
ims.AbstractPager.complete = function(){
	this.status = ims.Pager.statusComplete;
};

// 构造提交Form
ims.AbstractPager.buildSubmit = function(){
	var html = "";
	html += "<form id=" + this.formID + " method=post action=" + this.action + "> \n";
	html += "<input type=hidden name=pageStart> \n";
	html += "<input type=hidden name=pageSize> \n";
	html += "<input type=hidden name=pageCount value=" + this.pageCount + "> \n";
	html += this.parameter + " \n";
	html += "</form> \n";
	jQuery(html).appendTo(document.body);
};

// 输出分页条
ims.AbstractPager.printPager = function(){
	var html = "";
	html += "<div style='" + ims.Pager.cssPager + "'>";
	html += "<span name='reload' style='cursor:pointer;'>";
	html += "<span style='border:0;float:left;padding-top:4px;'><img src='" + ims.Pager.imgReload + "'/></span>\n";
	html += "<span style='float:left;padding-top:5px'>刷新</span>";
	html += "</span>";
	html += "<span name='export' style='cursor:pointer'>";
	html += "<span style='border:0;float:left;padding-top:1px'>&nbsp;<img src='" + ims.Pager.imgExcel + "'/></span>\n";
	html += "<span style='float:left;padding-top:5px'>导出</span> \n";
	html += "</span>";
	html += "<span style='float:right'>";
	html += "共<span style='" + ims.Pager.cssNumber + "'>" + this.pageCount + "</span>条记录 共<span style='" + ims.Pager.cssNumber + "'>" + this.getMaxPage() + "</span>页";
	html += "<img name='first' style='" + ims.Pager.imgCss + "' align=absmiddle src='" + ims.Pager.imgFirst + "'/> \n";
	html += "<img name='prev' style='" + ims.Pager.imgCss + "' align=absmiddle src='" + ims.Pager.imgPrev + "'/> \n";
	html += "<img name='next' style='" + ims.Pager.imgCss + "' align=absmiddle src='" + ims.Pager.imgNext + "'/> \n";
	html += "<img name='last' style='" + ims.Pager.imgCss + "' align=absmiddle src='" + ims.Pager.imgLast + "'/> \n";
	html += "第<input style='width:20px;height:18px;' name='pageNo' value='" + this.getCurPage() + "'>页\n";
	html += "<img name='goto' align=absmiddle src='" + ims.Pager.imgGoto + "'/> \n";
	html += "每页显示<select style='height:18px;' name='pageSize'> \n";
	for( var i = 0 ; i < this.pages.length ; i++ ){
		var isMatch = this.pageSize == this.pages[i];
		html += "<option value=" + this.pages[i] + " " + ( isMatch ? "selected" : "" ) + ">";
		html += this.pages[i] + "条</option> \n";
	}
	html += "</select>\n";
	html += "</span>";
	html += "</div>";
	jQuery("#" + this.pagerID).html(html);
};

// 输出分页条
ims.AbstractPager.printLoading = function(){
	var html = "";
	html += "<div align='center'>";
	html += "<span style=''><img name='loading' height='16' align=absmiddle src='" + ims.Pager.imgLoading + "'/></span> <span style='color:#6E6E6E;'>正在加载分页条.....</span>\n";
	html += "</div>";
	jQuery("#" + this.pagerID).html(html);
};

// 绑定常用事件
ims.AbstractPager.bindEvent = function(){
	var _this = this;
	var pager = jQuery("#" + this.pagerID);
	pager.find("img[name='first']").click(function(){
		_this.first();
	});
	pager.find("img[name='last']").click(function(){
		_this.last();
	});
	pager.find("img[name='next']").click(function(){
		_this.next();
	});
	pager.find("img[name='prev']").click(function(){
		_this.prev();
	});
	pager.find("input[name='pageNo']").change(function(){
		if( isNaN(this.value) ){		
			this.value = "";
		}else if( new Number(this.value) > _this.getMaxPage() ){
			this.value = _this.getMaxPage();
		}else if( new Number(this.value) < 1 ){
			this.value = 1;
		}
	});
	// 为什么jQuery.change( function(){});事件表现出来的行为不对呢？
	pager.find("select[name='pageSize']").get(0).onchange=function(){				 
		_this.go(1,this.value);
	};
	pager.find("img[name='goto']").click(function(){
		var pageNo = pager.find("input[name='pageNo']").val();
		if( pageNo )
			_this.go(pageNo,_this.pageSize);
	});
	pager.find("span[name='export']").click(function(){
		_this.Export();
	});
	pager.find("span[name='reload']").click(function(){
		_this.go(_this.getCurPage(),_this.pageSize);
	});
};

// 总页数
ims.AbstractPager.getMaxPage = function(){
	return (this.pageSize != 0  ? Math.ceil(this.pageCount/this.pageSize) : 1);
};

// 当前页数
ims.AbstractPager.getCurPage = function(){
	return (this.pageSize != 0  ? Math.ceil(this.pageStart/this.pageSize) : 1);
};

// 下一页
ims.AbstractPager.next = function(){
	if(this.getCurPage() + 1 > this.getMaxPage())
		return false;
	var pageNo = this.getCurPage() + 1;
	this.go(pageNo,this.pageSize);
};

// 上一页
ims.AbstractPager.prev = function(){
	if(this.getCurPage() == 1)
		return false;
	var pageNo = this.getCurPage() - 1;
	this.go(pageNo,this.pageSize);
};

// 第一页
ims.AbstractPager.first = function(){
	if(this.getCurPage() == 1 )
		return false;
	this.go(1,this.pageSize);
};

// 最后一页
ims.AbstractPager.last = function(){
	if(this.getCurPage() == this.getMaxPage())
		return false;
	var pageNo = this.getMaxPage();
	this.go(pageNo,this.pageSize);
};

// 开始go 
ims.AbstractPager.go = function( pageNo , pageSize ){
	// 如果允许分页
	if( this.isPage ){
		var pageStart = (pageNo-1) * this.pageSize + 1;
		jQuery("#" + this.formID + " input[name='pageStart']").val(pageStart);
		jQuery("#" + this.formID + " input[name='pageSize']").val(pageSize);
		jQuery("#" + this.formID).submit();
		this.loading();
	}
};

//加载进度条 o yeah!
ims.AbstractPager.loading = function(){
	var p = window.parent;
	try{
		if( p["startAction"] )
			p["startAction"]();
		else if( p["loadProcessPage"] )
			p["loadProcessPage"]();
	}catch(e){
	}
};

//是否加载完成
ims.AbstractPager.isComplete = function(){
	return (ims.Pager.statusComplete == this.status);
};

//取得总数
ims.AbstractPager.getCount = function(){
	return this.pageCount;
};

//导出方法，需要自己去实现
ims.AbstractPager.Export = function(){};

// 继承ims.AbstractPager
jQuery.extend(ims.Pager.prototype,ims.AbstractPager);